package com.dz.usersystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.dz.usersystem.entity.FilmUser;
import com.dz.usersystem.entity.R;
import com.dz.usersystem.service.FilmUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class Login {
    /**
     * 服务对象
     */
    @Resource
    private FilmUserService filmUserService;

    @GetMapping("login")
    @Transactional
    public List<FilmUser> login
            (@RequestParam(value = "username",required = true) String username,
             @RequestParam(value = "password",required = true) String password) {

        FilmUser user =new FilmUser();
        user.setUsername(username);
        user.setPassword(password);
        List<FilmUser> filmUsers = filmUserService.queryAll(user);
        if (filmUsers.size()> 0){
            FilmUser us=new FilmUser();
            String id =filmUsers.get(0).getId();
            Integer state=1;
            us.setId(id);
            us.setState(state);

            filmUserService.update(us);
        }
        return filmUsers;
    }

    @GetMapping("phoneLogin")
    public void phoneLogin
            () {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9oRpZFMZoDedBwGrFF", "nlsy0epfJFvQTxxI1SD2I1CV1hmqmV");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "15063207369");
        request.putQueryParameter("SignName", "zhao");
        request.putQueryParameter("TemplateCode", "200");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
